<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/11
 * Time: 16:37
 */

namespace App\Http\Logics\MiniProgramAdmin;

use App\Recruitment;
use App\Http\Logics\BaseLogic;
class RecruitmentLogic extends BaseLogic
{
    public static function createRecruitment($data)
    {
        $recruitment = new Recruitment();
        $recruitment->cafe_id = $data['cafe_id'];
        $recruitment->title = $data['title'];
        $recruitment->post = $data['post'];
        $recruitment->number = $data['number'];
        $recruitment->type = $data['type'];
        $recruitment->salary = $data['salary'];
        $recruitment->sex = $data['sex'];
        $recruitment->experience = $data['experience'];
        $recruitment->remark = $data['remark'];
        if (!$recruitment->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function updateRecruitment($data)
    {
        $recruitment = Recruitment::find($data['recruitment_id']);
        $recruitment->title = $data['title'];
        $recruitment->post = $data['post'];
        $recruitment->number = $data['number'];
        $recruitment->type = $data['type'];
        $recruitment->salary = $data['salary'];
        $recruitment->sex = $data['sex'];
        $recruitment->experience = $data['experience'];
        $recruitment->remark = $data['remark'];
        if (!$recruitment->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 获取招聘信息列表
     * @param $data
     * @return mixed
     */
    public static function getRecruitmentList($data)
    {
        $query = Recruitment::with(['cafe','cafe.city'])->where(function ($query) use ($data){
            if (isset($data['cafe_id']) && $data['cafe_id']){
                $query->where('cafe_id', $data['cafe_id']);
            }
            if (isset($data['status']) && !empty($data['status'])){
                $query->where('status', $data['status']);
            }
        })->orderBy('id', 'desc');
        $recruitment = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($recruitment)){
            return self::response(self::CONTENT_IS_EMPTY);
        }

        $recruitment->map(function ($recruitment) {
            return $recruitment->append('post_text')->append('sex_text')->append('salary_text')->append('experience_text');
        });
        return self::response(self::SUCCESS, $recruitment);
    }

    /**
     * 返回发布招聘信息所有的select选项
     * @return mixed
     */
    public static function getSelectList()
    {
        $data = [
            'post_select' => arrToSelect(Recruitment::$post_array),
            'salary_select' => arrToSelect(Recruitment::$salary_array),
            'experience_select' => arrToSelect(Recruitment::$experience_array),
            'sex_select' => arrToSelect(Recruitment::$sex_array)
        ];
        return self::response(self::SUCCESS, $data);
    }

    /**
     * 改变招聘信息的状态
     * @param $data
     * @return mixed
     */
    public static function changeStatus($data)
    {
        $recruitment = Recruitment::find($data['recruitment_id']);
        if (empty($recruitment)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $recruitment->status = !$recruitment->status;
        if (!$recruitment->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 招聘信息详情
     * @param $data
     * @return mixed
     */
    public static function getRecruitmentDetail($data)
    {
        $recruitment = Recruitment::with(['cafe'])->find($data['recruitment_id']);
        if (empty($recruitment)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $recruitment->append('post_text')->append('sex_text')->append('salary_text')->append('experience_text');
        // 浏览量加一
        $recruitment->pv = $recruitment->pv + 1;
        if (!$recruitment->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS, $recruitment);
    }
}